package com.pxmeta.common.utils.agora;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Maps;
import com.pxmeta.common.utils.HttpUtils;
import lombok.SneakyThrows;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URI;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class AgoraClient {

    @Value("${agora.customerKey}")
    private String customerKey;
    @Value("${agora.customerSecret}")
    private String customerSecret;
    @Value("${agora.baseUrl}")
    private String baseUrl;
    @Value("${agora.appId}")
    private String appId;
    @Value("${agora.certificate}")
    private String certificate;




    private static final int expirationTimeInSeconds = 3600 * 72;


    private  Map<String, String> getHeader(){
        // 拼接客户 ID 和客户密钥并使用 base64 编码
        String plainCredentials = customerKey + ":" + customerSecret;
        String base64Credentials = new String(Base64.getEncoder().encode(plainCredentials.getBytes()));
        // 创建 authorization header
        String authorizationHeader = "Basic " + base64Credentials;

        Map<String, String> map = new HashMap<>();
        map.put("Authorization",authorizationHeader);
        map.put("Content-Type", "application/json");
        return map;
    }

    private String getUrl(String path){
        return baseUrl + "/cn/v1.0/projects/"+ appId + path;
    }

    @SneakyThrows
    public  JSONObject doGetForMusic(String path, Map<String, String> params) {
        String url = getUrl(path);
        params.put("requestId", UUID.randomUUID().toString());
        JSONObject jsonObject = HttpUtils.doGet(url, params, getHeader());
        return jsonObject.getJSONObject("data");
    }
    @SneakyThrows
    public  JSONObject doGetForMusic(String path) {
        Map<String, String> params = Maps.newHashMap();
        String url = getUrl(path);
        params.put("requestId", UUID.randomUUID().toString());
        JSONObject jsonObject = HttpUtils.doGet(url, params, getHeader());
        return jsonObject;
    }

    public JSONObject getChannelList(Integer page, Integer size){
        String url = baseUrl + "/dev/v1/channel/" + appId;
        Map<String, String> param = Maps.newHashMap();
        param.put("page_no",String.valueOf(page-1));
        param.put("page_size",String.valueOf(size));
        JSONObject data = HttpUtils.doGet(url, param, getHeader()).getJSONObject("data");
        return data;
    }

    public JSONObject getUsers(String channelId){
        String url = baseUrl + "/dev/v1/channel/user/" + appId + "/" + channelId;

        JSONObject jsonObject = HttpUtils.doGet(url, getHeader()).getJSONObject("data");

        return jsonObject;
    }



    public Map<String, Object> getRtcToken(String uid, String channelName){
        RtcTokenBuilder token = new RtcTokenBuilder();
        int timestamp = (int) (System.currentTimeMillis() / 1000 + expirationTimeInSeconds);

        String result = token.buildTokenWithUid(appId, certificate,
                channelName, uid, RtcTokenBuilder.Role.Role_Publisher, timestamp);

        HashMap<String, Object> map = Maps.newHashMap();
        map.put("token", result);
        map.put("expireSeconds", expirationTimeInSeconds);
        return map;
    }
}

